<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入类别"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图书编号" prop="bookId">
        <el-input
          v-model="queryParams.bookId"
          placeholder="请输入图书编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        <el-form-item label="读者编号" prop="readerId">
            <el-input
                    v-model="queryParams.readerId"
                    placeholder="请输入读者编号"
                    clearable
                    @keyup.enter="handleQuery"
            />
        </el-form-item>
      <el-form-item label="预约时间" prop="orderTime">
        <el-date-picker clearable
          v-model="queryParams.orderTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否出库" prop="orderIsout">
        <el-input
          v-model="queryParams.orderIsout"
          placeholder="请输入是否出库"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否入库" prop="orderIsin">
        <el-input
          v-model="queryParams.orderIsin"
          placeholder="请输入是否入库"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['reservation:reservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
      <el-row>
          <div class="divClass"><b>预约借阅订单</b></div>
      </el-row>

<!--      v-if="queryParams.readerId==2214 -->
    <el-table v-loading="loading" :data="reservationList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="类别" align="center" prop="categoryId" />
      <el-table-column label="图书编号" align="center" prop="bookId" />
      <el-table-column label="读者编号" align="center" prop="readerId" />
      <el-table-column label="预约时间" align="center" prop="orderTime" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约起始时间" align="center" prop="orderBeginTime" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderBeginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约结束时间" align="center" prop="orderFinishTime" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderFinishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否出库" align="center" prop="orderIsout" />
      <el-table-column label="出库时间" align="center" prop="orderOuttime" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderOuttime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否入库" align="center" prop="orderIsin" />
      <el-table-column label="入库时间" align="center" prop="orderIntime" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderIntime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改借阅信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="reservationRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Reservation">
import { listReservation, getReservation, delReservation, addReservation, updateReservation } from "@/api/reservation/reservation";
import {parseTime} from "../../../utils/ruoyi.js";
import {getUserProfile} from "@/api/system/user.js";

const { proxy } = getCurrentInstance();

const reservationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
// const state = reactive({
//     user: {},
//     roleGroup: {},
//     postGroup: {},
//     userId:null
// });

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    categoryId: null,
    bookId: null,
    readerId: null,
    orderTime: null,
    orderIsout: null,
    orderIsin: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询当前用户信息 */
// function getUser() {
//     getUserProfile().then(response => {
//         console.info(response)
//         state.user = response.data;
//         state.roleGroup = response.roleGroup;
//         state.postGroup = response.postGroup;
//         state.userId = response.data.userId;
//         console.info(state.userId)
//     });
// };

/** 查询借阅信息列表 */
function getList() {
    const state = reactive({
        user: {},
        roleGroup: {},
        postGroup: {},
        userId:null
    });
  loading.value = true;
  getUserProfile().then(response => {
        console.info(response)
        state.user = response.data;
        state.roleGroup = response.roleGroup;
        state.postGroup = response.postGroup;
        state.userId = response.data.userId;
        console.info(state.userId)

        // queryParams.value.readerId =state.userId;
        console.info(queryParams.value.readerId)

      listReservation(queryParams.value).then(response => {
          reservationList.value = response.rows;
          total.value = response.total;
          loading.value = false;
      });
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    orderId: null,
    categoryId: null,
    bookId: null,
    readerId: null,
    orderTime: null,
    orderBeginTime: null,
    orderFinishTime: null,
    orderIsout: null,
    orderOuttime: null,
    orderIsin: null,
    orderIntime: null,
    orderInManager: null
  };
  proxy.resetForm("reservationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加借阅信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _orderId = row.orderId || ids.value
  getReservation(_orderId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改借阅信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reservationRef"].validate(valid => {
    if (valid) {
      if (form.value.orderId != null) {
        updateReservation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReservation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value;
  proxy.$modal.confirm('是否确认删除借阅信息编号为"' + _orderIds + '"的数据项？').then(function() {
    return delReservation(_orderIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('reservation/reservation/export', {
    ...queryParams.value
  }, `reservation_${new Date().getTime()}.xlsx`)
}

getList();
// getUser();
</script>

<style scoped>

.divClass{
    width: 100%;
    height: 40px;
    background-color: #bfbfbf;
    text-align: center;
    line-height: 40px;
    color: #333333;
}

</style>
